var app = getApp();
Page({
  data: {
    owner_store: {},
    store_id: "",
    hostImg: wx.getStorageSync("yz_basic_info").host + "/store-code/group.png",
    code: "",
    clickTag: false,
    showBind: false,
    nobind: false
  },
  onLoad(options) {
    if (options.scene) {
      let scene = decodeURIComponent(options.scene);
      console.log(scene,'222222222222222222222222222')
      let newscene = scene.split("=");
      if (newscene[0] == "code") {
        this.setData({
          code: newscene[1],
        });
      }
    }
    if (options.code) {
      this.setData({
        code: options.code,
      });
    }
    this.data.clickTag = false;
    this.getStoreOid();
  },
  onShareAppMessage(e) {
    var value = wx.getStorageSync("yz_uid");
    var mid = "";
    if (value) {
      mid = value;
    }
    return {
      path: `/mircoApp/store-code/storeCodeindex?code=${this.data.code}&mid=${mid}`
    }
  },
  async getStoreOid() {
    let {
      data
    } = await app._postNetWork({
      url: app.getNetAddresss("plugin.store-code.api.code.no-auth-info"),
      data: {
        code: this.data.code,
        basic_info: 0
      },
    });
    if (data.result) {
      let id = data.data.store_owner_id;
      if(!this.options.mid && id){
        app._setMid(id);
      }
      this.getData()
    } else {
      app.tips(data.msg);
    }
  },
  async codeBind() {
    if (!this.data.owner_store) return;
    if (this.data.store_id) {
      app.tips("收款码已被绑定");
      return;
    }
    if (this.data.clickTag) return app.tips("请求中");
    this.data.clickTag = true;
    setTimeout(() => {
      this.data.clickTag = false;
    }, 1000);
    let {
      data
    } = await app._postNetWork({
      url: app.getNetAddresss("plugin.store-code.api.code.bind"),
      data: {
        code: this.data.code,
      },
    });
    if (data.result) {
      wx.showToast({
        title: data.msg,
        icon: "none",
        duration: 1000,
        success: () => {
          setTimeout(() => {
            wx.redirectTo({
              url: "/packageG/index/index",
            });
          }, 1000);
        },
      });
    } else {
      app.tips(data.msg);
    }
  },
  async getData() {
    this.setData({
      showBind: false,
      nobind: false
    })
    let {
      data
    } = await app._postNetWork({
      url: app.getNetAddresss("plugin.store-code.api.code.info"),
      data: {
        code: this.data.code,
      },
    });
    if (data.result) {
      this.setData({
        owner_store: data.data.owner_store,
        has_one_store: data.data.has_one_store,
        store_id: data.data.store_id,
      });
      if (!this.data.owner_store) {
        // 消费者扫码
        if (this.data.has_one_store) {
          //收款码已绑定商户，跳转空白页后可跳转收银台页面
          wx.navigateTo({
            url: `/packageC/cashier/cashier_pay/cashier_pay?store_id=${this.data.has_one_store.id}`,
          })
        } else {
          // 收款码未绑定商户，跳转空白页后，提示无法支付
          this.setData({
            nobind: true,
          });
        }
      } else {
        // 商户扫码
        if (this.data.has_one_store) {
          if (this.data.has_one_store.id == this.data.owner_store.id) {
            // 收款码已绑定自己的商户，跳转商户
            wx.navigateTo({
              url: `/packageC/o2o/HomeSeller/HomeSeller?store_id=${this.data.has_one_store.id}`,
            })
          } else {
            // 收款绑定的不是自己的商户，去收银台页面
            wx.navigateTo({
              url: `/packageC/cashier/cashier_pay/cashier_pay?store_id=${this.data.has_one_store.id}`,
            })
          }
        } else {
          //收款码未绑定商户，跳转空白页后，跳转绑定页面
          this.setData({
            showBind: true,
          });
        }
      }
    } else {
      app.tips(data.msg);
    }
  },
});